﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;

namespace Eugenhome.Atrium.FotoSlider.Web
{
    /// <summary>
    /// 
    /// </summary>
    public partial class SliderEdit : System.Web.UI.Page
    {
        /// <summary></summary>
        public string SelectedImage { get; set; }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!IsPostBack)
            {
                //Server-Bilder aktualisieren und anzeigen
                UpdateServerImages();
            }
        }

        /// <summary>
        /// 
        /// </summary>
        private void UpdateServerImages()
        {
            lstDailyImages.DataSource = new SliderFile("").GetFiles(Server.MapPath(""));
            lstDailyImages.DataTextField = "DisplayName";
            lstDailyImages.DataValueField = "FullFileName";
            lstDailyImages.DataBind();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void lstDailyImages_SelectedIndexChanged(object sender, EventArgs e)
        {
            SelectedImage = new SliderFile(lstDailyImages.SelectedValue).HttpPath;
            this.DataBind();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnFileUpload_Click(object sender, EventArgs e)
        {
            if (fileUpload.HasFile)
                try
                {
                    string targetpath = Server.MapPath(Path.Combine(SliderFile.IMAGEFOLDER, fileUpload.FileName));
                    fileUpload.SaveAs(targetpath);
                }
                catch (Exception ex)
                {
                    //Label1.Text = "ERROR: " + ex.Message.ToString();
                }
            else
            {
                // Label1.Text = "You have not specified a file.";
            }

            //Server-Bilder aktualisieren und anzeigen
            UpdateServerImages();
        }

        /// <summary>
        /// 
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        protected void btnDelete_Click(object sender, ImageClickEventArgs e)
        {
            if (lstDailyImages.SelectedValue != null && File.Exists(lstDailyImages.SelectedValue))
            {
                try
                {
                    File.Delete(lstDailyImages.SelectedValue);
                }
                catch { }

                //Server-Bilder aktualisieren und anzeigen
                UpdateServerImages();
            }

        }
    }
}